home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / cmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.9 KB  |  166 lines

  1. /*++
  2. /* NAME
  3. /*    cmail 1
  4. /* SUMMARY
  5. /*    report if there are unread messages
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    cmail
  10. /* SYNOPSIS
  11. /*    cmail [-p password]
  12. /* DESCRIPTION
  13. /*    cmail reports if there are unread mail messages in the
  14. /*    pc-mail spool directory.
  15. /*
  16. /*    If the -p option is present, cmail first invokes the cico
  17. /*    program to contact the mail server host.
  18. /*
  19. /*    Typically cmail is run immediately after system startup,
  20. /*    while you are getting your first cup of coffee.
  21. /*
  22. /*    The program returns a nonzero exit status if it could not
  23. /*    find any mail.
  24. /* COMMANDS
  25. /*    cico    file transfer program
  26. /*    nmail    processes new mail
  27. /* FILES
  28. /*    Various files in the spool directory
  29. /*
  30. /*    LOGFILE system status messages
  31. /*    n<seqno> mail messages
  32. /*    h<seqno> header line of new mail
  33. /*    o<seqno> header line of old mail
  34. /* DIAGNOSTICS
  35. /*    Error messages in case the environment is not properly set up.
  36. /* BUGS
  37. /*    Invites people to put their mail password into the autoexec file.
  38. /* AUTHOR(S)
  39. /*    W.Z. Venema
  40. /*    Eindhoven University of Technology
  41. /*    Department of Mathematics and Computer Science
  42. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  43. /* CREATION DATE
  44. /*    Sun Apr  3 19:34:57 MET 1988
  45. /* LAST MODIFICATION
  46. /*    90/01/22 13:01:19
  47. /* VERSION/RELEASE
  48. /*    2.1
  49. /*--*/
  50.  
  51. #include <stdio.h>
  52. #include <signal.h>
  53. #include <varargs.h>
  54.  
  55. #include "defs.h"
  56. #include "ndir.h"
  57. #include "path.h"
  58. #include "status.h"
  59.  
  60. hidden void parse_args();        /* forward declarations */
  61. hidden void usage();
  62. hidden void error();
  63. hidden int newmail();
  64.  
  65. hidden char *password = 0;        /* set by the -p option */
  66.  
  67. public char *progname = "cmail";    /* for diagnostics */
  68.  
  69. main(argc, argv)
  70. int     argc;
  71. char  **argv;
  72. {
  73.     signal(SIGINT, SIG_IGN);            /* disable ctrl-c */
  74.     parse_args(argc, argv);            /* parse command args */
  75.     if (pathinit())                /* check path info */
  76.     error("cmail: bad MAILDIR environment variable");
  77.     if (password && *password &&
  78.     invokelp(CICO, "-p", password, (char *) 0) == E_NOPROG)
  79.     error("cmail: cannot execute the CICO program");
  80.     if (invokelp(NMAIL, (char *) 0) == E_NOPROG)
  81.     error("cmail: cannot execute the NMAIL program");
  82.     exit(newmail() == 0);            /* look for new mail */
  83. }
  84.  
  85. /* parse_args - process command-line arguments */
  86.  
  87. hidden void parse_args(argc, argv)
  88. int     argc;
  89. char  **argv;
  90. {
  91.     while (--argc && *++argv && **argv == '-') {/* process options */
  92.     switch (*++*argv) {
  93.     case 'p':                /* call cico first */
  94.         if (--argc == 0)
  95.         usage("missing password");
  96.         password = *++argv;
  97.         break;
  98.     default:                /* unknown option */
  99.         usage("invalid option: -%s", *argv);
  100.         break;
  101.     }
  102.     }
  103.  
  104.     /* check for extraneous arguments */
  105.  
  106.     if (argc > 0)
  107.     usage("unexpected argument: %s", *argv);
  108. }
  109.  
  110. /* scan for new unread mail */
  111.  
  112. hidden int newmail()
  113. {
  114.     DIR    *dp;
  115.     struct direct *de;
  116.     FILE   *fp;
  117.     char    from[MAXLINE];
  118.     int     msgcount = 0;
  119.     unsigned msgno;
  120.  
  121.     /*
  122.      * Scan the spool directory for unread messages and extract the
  123.      * originator address from the corresponding meta file.
  124.      */
  125.  
  126.     if ((dp = opendir(maildir)) == 0) {
  127.     error("cmail: cannot access the mail directory");
  128.     } else {
  129.     while (de = readdir(dp)) {
  130.         if (de->d_name[0] == NEW_MESG
  131.         && (msgno = seqno(de->d_name))
  132.         && (fp = fopen(new_meta(msgno), "r")) != 0) {
  133.         if (fgets(from, sizeof(from), fp))
  134.             printf("You have new mail from %s", from);
  135.         msgcount++;
  136.         fclose(fp);
  137.         }
  138.     }
  139.     closedir(dp);
  140.     }
  141.     return (msgcount);
  142. }
  143.  
  144. hidden void error(str)
  145. char   *str;
  146. {
  147.     fprintf(stderr, "%s\n", str);
  148.     exit(1);
  149. }
  150.  
  151. /* VARARGS */
  152.  
  153. hidden void usage(va_alist) 
  154. va_dcl
  155. {
  156.     va_list ap;
  157.     char   *fmt;
  158.  
  159.     va_start(ap);
  160.     fmt = va_arg(ap, char *);
  161.     vfprintf(stderr, fmt, ap);
  162.     va_end(ap);
  163.     fprintf(stderr, "\nusage: cmail [-p password]\n");
  164.     exit(1);
  165. }
  166.